import 'package:flutter/material.dart';
import 'package:flutter_demo/viewmodel/timer_viewmodel.dart';
import 'package:provider/provider.dart';
import '../viewmodel/counter_viewmodel.dart';

class CounterView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Provider MVVM Counter Demo'),
      ),
      body: Center(
        child: Consumer<TimerViewModel>(
          builder: (context, model, child) {
            return ListView.builder(
                itemCount: model.list.length,
                itemBuilder: (context, position) {
                  return ListTile(
                    title: Text(
                      '第 $position 项  ${model.list[position].difference}',
                      style: Theme.of(context).textTheme.headlineMedium,
                    ),
                  );
                });
          },
        )
      ),
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          FloatingActionButton(
            onPressed: Provider.of<CounterViewModel>(context).increment,
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ),
          const SizedBox(height: 10),
          FloatingActionButton(
            onPressed: Provider.of<CounterViewModel>(context).decrement,
            tooltip: 'Decrement',
            child: const Icon(Icons.remove),
          ),
        ],
      ),
    );
  }
}
